/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.impl;

import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.cmmp.uac.dao.IPermissionManageDao;
import com.eelpo.cmmp.uac.dao.entity.TPermission;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.Join;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 * 
 * 权限管理持久层具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class PerssionManageDao extends BasicCommonDao implements IPermissionManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IPermissionManageDao#addPermission(com.eelpo
	 *      .cmmp.uac.dao.entity.TPermission)
	 */
	@Override
	public String addPermission(TPermission permission) throws DataAccessException {
		persist(permission);
		return permission.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IPermissionManageDao#checkExist(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public boolean checkExist(String entityId, String actionId) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TPermission.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("entity.id", entityId));
		criteria.add(Expression.eq("action.id", actionId));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IPermissionManageDao#getPermissions(java.lang.String)
	 */
	@Override
	public List<TPermission> getPermissions(String userId) throws ServiceException {
		Criteria criteria = JpaCriteria.create(TPermission.class, "p");
		criteria.result(Result.result());
		criteria.join(Join.joinFetch("entity e"));
		criteria.join(Join.joinFetch("action a"));
		criteria.join(Join.join("roles r"));
		criteria.join(Join.joinInornate("r.users u"));
		criteria.add(Expression.eqInornate("u.id", userId));
		criteria.add(Expression.eq("status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("r.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("e.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("a.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("u.status", Status.ENABLED.getValue()));
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IPermissionManageDao#getPermissions()
	 */
	@Override
	public List<TPermission> getPermissions() throws ServiceException {
		Criteria criteria = JpaCriteria.create(TPermission.class, "p");
		criteria.result(Result.result());
		criteria.join(Join.joinFetch("entity e"));
		criteria.join(Join.joinFetch("action a"));
		criteria.add(Expression.eq("status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("e.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("a.status", Status.ENABLED.getValue()));
		return getResultList(criteria);
	}
}
